////////////////////////////////////////////////////////////////
// filename: timer.cpp
// description: keep track of time
// autor: Jay Tomten (jaytomten@gmail.com)
// notes:

// namespace

// defines

// includes
#include <SDL/SDL.h>
#include "timer.h"

// globals

////////////////////////////////////////////////////////////////
// constructor
Timer::Timer() {

    fps = 0.0;
    frame = 0;
    currTime = 0;
    prevTime = 0;
    baseTime = 0;
    delta = 0.1f; // not 0, div by zero error

}
////////////////////////////////////////////////////////////////
// destructor
Timer::~Timer() {

    // nothing to delete

}

////////////////////////////////////////////////////////////////
// Update() - update all the private vars
bool Timer::Update(float d) {

    frame++;
    prevDelta = d; // just in case we need in the future, and for continuity :)
    currTime = SDL_GetTicks(); // set current time
    delta = ((float)currTime - (float)prevTime) / 1000.0000f; // set time since last frame (delta)
    prevTime = currTime;
    if(currTime - baseTime > 1000){
        fps = (frame * 1000.0000f)/((float)currTime - (float)baseTime);
        baseTime = currTime;
        frame = 0;
    }

    return true; // always

}

////////////////////////////////////////////////////////////////
// GetDelta() - return delta
float Timer::GetDelta() {

    return delta;

}

////////////////////////////////////////////////////////////////
// GetFPS() - return frames per second
float Timer::GetFPS(){

    return fps;

}

////////////////////////////////////////////////////////////////
// LockFPS() - don't let our game burn up a computer!
void Timer::LockFPS(float targetFPS){

    if (targetFPS == 0) targetFPS = 30;
    do currTime = SDL_GetTicks();
    while ((frame * 1000.0)/(currTime - baseTime) > (float)targetFPS);

}

